﻿using System;
using NAnt.Core;
using NAnt.Core.Attributes;
using NAnt.SVN.Tasks;
using NAnt.SVN.Util;
using NAnt.SVN.Tasks.Constants;

namespace NAnt.SVN.Attributes
{
    /// <summary>
    /// Validator for SVN Keywords
    /// $Id$
    /// </summary>
    public sealed class TrunkUriValidator : ValidatorAttribute
    {

        #region Public Instance Constructors

        /// <summary>
        /// Initializes a new instance of the <see cref="KeywordsValidator" /> 
        /// class.
        /// </summary>
        public TrunkUriValidator()
        {
        }

        #endregion Public Instance Constructors

        #region Override implementation of ValidatorAttribute

        /// <summary>
        /// Checks if the specified value can be converted to keywords
        /// </summary>
        /// <param name="value">The value to be checked.</param>
        /// <exception cref="ValidationException"><paramref name="value" /> cannot be converted to a <see cref="bool" />.</exception>
        public override void Validate(object value)
        {
            try
            {
                if (!UrlUtil.ContainsTrunk(new Uri((string)value)))
                {
                    throw new BuildException(string.Format(Strings.GetString("SVN0312"), (string)value));
                }
            }
            catch (Exception e)
            {
                throw new BuildException(string.Format(Strings.GetString("SVN0312"), (string)value), e);
            }
        }

        #endregion Override implementation of ValidatorAttribute
    }
}
